package com.geek.novel.activity.auth

import android.os.Bundle
import android.view.View
import com.geek.novel.R
import com.geek.novel.activity.auth.view.listener.PwdViewerClickListener
import com.geek.novel.activity.base.TitleBarActivity
import com.geek.novel.bo.AppContext
import com.geek.novel.common.Constant
import com.geek.novel.entity.SysUser
import com.geek.novel.error.ValidateException
import com.geek.novel.listener.ApiResultListener
import com.geek.novel.services.manager.AuthManager
import com.geek.novel.utils.ToastUtils
import com.geek.novel.utils.ValidateUtil
import com.geek.novel.vo.ApiResult
import kotlinx.android.synthetic.main.activity_regist.*

/**
 * 注册页面
 */
class RegistActivity: TitleBarActivity(), ApiResultListener {

	//用户信息管理器
	private lateinit var authManager: AuthManager

	override fun pageName(): String{
		return AppContext.getContext().getString(R.string.title_bar_page_regist)
	}
	
	override fun onCreate(savedInstanceState: Bundle?) {
		super.onCreate(savedInstanceState)
		setContentView(R.layout.activity_regist)

		//初始化用户信息管理器
		authManager = AuthManager()
		authManager.setOnApiResultListener(this)
	}

	/**附加控件*/
	override fun attachViewChild() {
		btn_regist.setOnClickListener(this)
		//显示隐藏密码
		iv_regist_password_visible.setOnClickListener(PwdViewerClickListener(et_regist_password))
		iv_regist_password_again_visible.setOnClickListener(PwdViewerClickListener(et_regist_password_again))

		//模拟注册数据，测试用，待删除
//		this.mockUserInfo()
	}

//	/**模拟注册数据，测试用，待删除*/
//	@SuppressLint("SetTextI18n")
//	private fun mockUserInfo(){
//		et_regist_phone.setText("15986423982")
//		et_regist_email.setText("674797425@qq.com")
//		et_regist_password.setText("A123456")
//		et_regist_password_again.setText("A123456")
//	}

	override fun clickEvent(id: Int, v: View) {
		when(id){
			//注册
			R.id.btn_regist->{
				this.regist()
			}
		}
	}

	/**
	 * 注册
	 */
	private fun regist() {
		if(!cb_regist_agreement.isChecked){
			ToastUtils.toast("请勾选用户协议再注册")
			return
		}
		tv_regist_error_message.visibility = View.GONE
		try {
			val user = SysUser()
			user.phone = et_regist_phone.text.toString()
			user.email = et_regist_email.text.toString()
			user.password = et_regist_password.text.toString()
			user.passwordAgain = et_regist_password_again.text.toString()
			user.username = user.phone
			user.name = ""

			//触发校验
			ValidateUtil.validatePhone(user.phone, "手机号码")
			ValidateUtil.validateEmail(user.email, "电子邮箱")
			ValidateUtil.validateNotEmpty(user.password, "密码", "请输入密码！")
			ValidateUtil.validatePassword(user.password, "密码")
			ValidateUtil.validateNotEmpty(user.passwordAgain, "密码确认", "请再次输入密码！")
			ValidateUtil.validatePassword(user.passwordAgain, "密码确认")
			if(user.password != user.passwordAgain){
				throw ValidateException("两次输入密码不一致！")
			}
			//发起请求
			authManager.regist(user)

		} catch (e: Exception) {
			tv_regist_error_message.visibility = View.VISIBLE
			tv_regist_error_message.text = e.message
		}

	}

	/**处理http请求响应结果*/
	override fun handleResult(result: ApiResult, code: Int) {
		when(code){
			Constant.HTTP_KEY_REGIST->{
				//做一次获取用户信息的调用
				authManager.getUserInfo()
			}
			Constant.HTTP_KEY_GET_USERINFO->{
				ToastUtils.toast("注册成功！")
				setResult(RESULT_OK, intent)
				finish()
			}
		}
	}

}